library(spectral)
library(tuneR)
library(seewave)
time_series <- rnorm(100)
#Calculation of the Power Spectral Density (PSD)
psd_result <- periodogram(time_series,type="power",method="periodogram",
                          plot=FALSE)

library(psd)
#Assuming that x is a time series dataset...
x <- read.csv("J1.csv")
x <- data.matrix(x)
#Compute the PSD.
psd_result <- pspectrum(x)
#Plot the PSD.
plot(psd_result,log="dB")

##The spect() function
#Load the stats package using library(stats).
library(stats)
library(gsignal)
library(pracma)
#Create a time series dataset.
time_series <- rnorm(100)
#Compute the autocorrelation function.
acf(time_series)
#The spec() function is used to compute the periodogram.
periodogram_result <- spectrum(time_series)
plot(periodogram_result)
#The power spectrum was estimated using Welch's method with the pwelch() function.
pwelch_result <- pwelch(time_series,window = nextpow2(sqrt(NROW(time_series))),
                        overlap = 0.5,nfft = length(window),fs=1,detrend = "long-mean")
plot(pwelch_result)

library(base)
library(pracma)
library(reader)

data <- read.csv("J7.csv")
data <- data$J7
n <- length(data)
t <- 1:n
N <- 300
f <- linspace(0,1,N+1)
x <- data-mean(data)
plot(t,x,type="l")
zeros_vector <- rep(0,N-length(x))
y <- c(x,zeros_vector)
Y=fft(y)
#Compute the complex conjugate.
Y_conj <- Conj(Y)
#Compute the dot product.
dot_product <- Re(Y*Y_conj)
#Compute the Power Spectral Density (PSD).
P <- dot_product/N
#Create a sequence from 1 to N/2 (excluding N/2 if N is odd).
D <- P[seq(1,by=1,length.out=N/2)]
#If N is odd, the middle element should be excluded.
if(N %% 2 != 0){
  #Create a sequence from 1 to (N-1)/2.
  D <- P[seq(1,by=1,length.out=(N-1)/2)]
}
x_data <- f[1:(N/2)+1]
plot(x_data,D,type="l")
XP <- x_data
YP <- D
dataP <- cbind(XP,YP)
write.csv(dataP,"J7PSA.csv")


